package service

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	pay_pb "bmember/api/pb/client/pay/api/pay"

	"bmember/pkg/util"

	"bmember/internal/object"

	pb "bmember/api/pb/server"

	"bmember/internal/dao"
	"bmember/internal/model"
	"bmember/pkg/lueerror"
)

type assetSrv struct {
	*Service
	name string
}

var oSrv *assetSrv

// InitAssetSrv  初始化订单相关逻辑层定义
func InitAssetSrv() {
	oSrv = &assetSrv{
		Service: service,
		name:    "asset_service",
	}
}

// GetAssetSrv 获得订单相关逻辑层定义
func GetAssetSrv() *assetSrv {
	return oSrv
}

func (srv *assetSrv) getAccountById(ctx context.Context, memberId, accId int64, isLock bool) (*model.MemberAccount, lueerror.LueError) {
	var acc *model.MemberAccount
	var err error
	if isLock {
		acc, err = dao.GetMemberAccountDao().LockAccById(ctx, accId)
	} else {
		acc, err = dao.GetMemberAccountDao().GetAccById(ctx, accId)
	}
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if acc == nil || acc.MemberId != memberId {
		return nil, lueerror.AccNotExit(fmt.Errorf("会员 %d 账户 %d 不存在", memberId, accId))
	}
	if !acc.IsValid() {
		return nil, lueerror.InvalidAccount(fmt.Errorf("会员 %d 账户 %d 不可用", memberId, accId))
	}
	return acc, nil
}

// 一个品牌目前来说只有一个体系
func (srv *assetSrv) getBrandGroup(ctx context.Context, brandId int64) (*model.AccountGroup, lueerror.LueError) {
	groups, err := dao.GetCardDao().GetAccountGroupByBrandId(ctx, brandId)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if len(groups) == 0 {
		return nil, lueerror.GroupNotExist(fmt.Errorf("体系不存在"))
	}
	return groups[0], nil
}

// 根据会员id ，体系获取账号
func (srv *assetSrv) getAccByMemberGroup(ctx context.Context, memberId, groupId int64, isLock bool) (*model.MemberAccount, lueerror.LueError) {
	var acc *model.MemberAccount
	var err error
	if isLock {
		acc, err = dao.GetMemberAccountDao().LockAccByMemberGroup(ctx, memberId, groupId)
	} else {
		acc, err = dao.GetMemberAccountDao().GetAccByMemberGroup(ctx, memberId, groupId)
	}
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(fmt.Errorf("会员 %d 体系下 %d 不存在账户", memberId, groupId))
	}
	if !acc.IsValid() {
		return nil, lueerror.InvalidAccount(fmt.Errorf("会员 %d 体系下 %d 账户不可用", memberId, groupId))
	}
	return acc, nil
}

func (srv *assetSrv) getAccountByGroup(ctx context.Context, memberId, groupId int64) (*model.MemberAccount, lueerror.LueError) {
	acc, err := dao.GetMemberAccountDao().GetAccByMemberGroup(ctx, memberId, groupId)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if acc == nil {
		return nil, lueerror.AccNotExit(fmt.Errorf("会员 %d 不存在体系 %d 下账户", memberId, groupId))
	}
	return acc, nil
}

// 获取卡定义
func (srv *assetSrv) getCardInfo(ctx context.Context, cardId int64) (*model.CardAgg, lueerror.LueError) {
	card, err := dao.GetCardDao().GetCardAggByCardId(ctx, cardId)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if card == nil {
		return nil, lueerror.CardNotExist(fmt.Errorf("卡 %d 定义不存在", cardId))
	}
	return card, nil
}

// IntegralAward 积分赠送
func (srv *assetSrv) IntegralAward(ctx context.Context, brandId int64, p *pb.IntegralAwardReq, empId int64) (*pb.IntegralAwardReply, lueerror.LueError) {
	if p == nil {
		return nil, nil
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 锁定账号
		var err error
		var acc *model.MemberAccount
		var errL lueerror.LueError
		if p.AccId != 0 {
			acc, errL = srv.getAccountById(ctx, p.MemberId, p.AccId, true)
		} else {
			// 按体系查找
			group, errT := srv.getBrandGroup(ctx, brandId)
			if errT != nil {
				return nil, errT
			}
			acc, errL = srv.getAccByMemberGroup(ctx, p.MemberId, group.Id, true)
		}
		if errL != nil {
			return nil, errL
		}
		// 赠送积分
		acc.AwardIntegral(p.Num)
		err = dao.GetMemberAccountDao().UpdateAccIntegralById(ctx, acc)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		// 积分赠送记录
		iLog := model.NewIntegralLog(brandId, p.StoreId, acc.GroupId, acc.CardId, acc.MemberId, acc.Id, empId)
		// 积分值
		iLog.Award(p.Num)
		// 积分原因
		iLog.SetReason(p.Reason)
		iLog.Id, err = dao.GetIntegralLogDao().CreateIntegralLog(ctx, iLog)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		return iLog, nil
	})
	if err != nil {
		return nil, parseLueerror(err)
	}
	if ret == nil {
		return nil, nil
	}
	iLog := ret.(*model.IntegralLog)
	return &pb.IntegralAwardReply{
		IntegralId: iLog.Id,
		Message:    "OK",
	}, nil
}

func (srv *assetSrv) toIntegralInfoReply(iLog *model.IntegralLog) *pb.IntegralInfoReply {
	if iLog == nil {
		return nil
	}
	return &pb.IntegralInfoReply{
		IntegralId: iLog.Id,
		MemberId:   iLog.MemberId,
		GroupId:    iLog.GroupId,
		BrandId:    iLog.BrandId,
		IntType:    iLog.GetIntegralType(),
		Num:        iLog.Num,
		CreatedAt:  util.TimeStdFormat(iLog.CreatedAt),
	}
}

// GetIntegralRecords 获取积分记录
func (srv *assetSrv) GetIntegralRecords(ctx context.Context, brandId, memberId int64, pg object.Pagination) (*pb.IntegralListReply, lueerror.LueError) {
	group, errL := srv.getBrandGroup(ctx, brandId)
	if errL != nil {
		return nil, errL
	}
	mIds, err := dao.GetLoginChannelDao().GetRelatedMemberId(ctx, brandId, memberId)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	iLogs, total, err := dao.GetIntegralLogDao().GetIntegralsByMembersGroup(ctx, mIds, group.Id, pg)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	rLogs := make([]*pb.IntegralInfoReply, len(iLogs))
	for idx, i := range iLogs {
		rLogs[idx] = srv.toIntegralInfoReply(i)
	}
	return &pb.IntegralListReply{
		Total:     total,
		PageNo:    pg.PageNo,
		PageSize:  pg.PageSize,
		Integrals: rLogs,
	}, nil
}

// 按支付单计算支付单积分额
func (srv *assetSrv) calIntegralByPayOrder(ctx context.Context, payOrderNo string) (uint64, lueerror.LueError) {
	iLogs, err := dao.GetIntegralLogDao().GetIntegralLogByOrder(ctx, payOrderNo)
	if err != nil {
		return 0, lueerror.DbErr(err)
	}
	var sum int64
	for _, l := range iLogs {
		sum += l.Num
	}
	if sum < 0 {
		sum = -sum
	}
	return uint64(sum), nil
}

// IntegralByPayOrderNotify 根据支付单消息积分
func (srv *assetSrv) IntegralByPayOrderNotify(ctx context.Context, memberId, accId int64, cardId int64, p *pay_pb.PayOrderView) (*pb.IntegralInfoReply, lueerror.LueError) {
	card, errL := srv.getCardInfo(ctx, cardId)
	if errL != nil {
		return nil, errL
	}
	// 是否可以积分
	if !card.Card.CanIntegral {
		return nil, nil
	}
	// 判断卡是否适用该门店
	if p.StoreId != 0 && !card.Group.CanUseInStore(int64(p.StoreId)) {
		return nil, nil
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 获取积分账号
		acc, errL := srv.getAccountById(ctx, memberId, accId, false)
		if errL != nil {
			return nil, errL
		}
		if cardId != acc.CardId {
			return nil, lueerror.CardNotExist(fmt.Errorf("会员 %d 未持有会员卡 %d", memberId, cardId))
		}
		// 判断是否已经积分
		intNum, errL := srv.calIntegralByPayOrder(ctx, p.OrderNumber)
		if errL != nil {
			return nil, errL
		}
		// 已经积过分,目前为止，一个单只能积分一次
		if intNum > 0 {
			return nil, nil
		}
		// 计算积分数
		num := card.Card.CalculateIntegral(p.UserPayed)
		if num == 0 {
			return nil, nil
		}
		// 更新账号积分
		acc.ConsumptionIntegral(num)
		err := dao.GetMemberAccountDao().UpdateAccIntegralById(ctx, acc)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		// 积分记录
		iLog := model.NewIntegralLog(acc.BrandId, int64(p.StoreId), acc.GroupId, acc.CardId, acc.MemberId, acc.Id, 0)
		iLog.Consumption(num, p.UserPayed, p.OrderNumber)
		iLog.Id, err = dao.GetIntegralLogDao().CreateIntegralLog(ctx, iLog)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		return iLog, nil
	})
	if err != nil {
		return nil, parseLueerror(err)
	}
	if ret == nil {
		return nil, nil
	}
	return srv.toIntegralInfoReply(ret.(*model.IntegralLog)), nil
}

// IntegralByRefundOrderNotify 根据退单消息扣积分
func (srv *assetSrv) IntegralByRefundOrderNotify(ctx context.Context, memberId, accId int64, cardId int64, p *pay_pb.RefundOrderView) (*pb.IntegralInfoReply, lueerror.LueError) {
	if p == nil {
		return nil, nil
	}
	card, errL := srv.getCardInfo(ctx, cardId)
	if errL != nil {
		return nil, errL
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 获取积分账号
		acc, errL := srv.getAccountById(ctx, memberId, accId, true)
		if errL != nil {
			return nil, errL
		}
		// 计算积分数
		num := card.Card.CalculateIntegral(p.UserPayed)
		if num == 0 {
			return nil, nil
		}
		// 检查已经可退积分分值,
		intSum, errL := srv.calIntegralByPayOrder(ctx, p.PayOrderNumber)
		if errL != nil {
			return nil, errL
		}
		// 退积分数大于已经积分数，积分全扣
		if intSum < num {
			num = intSum
		}
		// 账户积分数，小于需要扣的积分数，积分全扣
		if acc.Integral < num {
			num = acc.Integral
		}
		if num == 0 {
			return nil, nil
		}
		// 扣除账号积分
		acc.RefundConsumptionIntegral(num)
		err := dao.GetMemberAccountDao().UpdateAccIntegralById(ctx, acc)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		// 积分记录
		iLog := model.NewIntegralLog(acc.BrandId, int64(p.StoreId), acc.GroupId, acc.CardId, acc.MemberId, acc.Id, 0)
		iLog.ConsumptionRefund(num, p.UserPayed, p.PayOrderNumber, p.OrderNumber)
		iLog.Id, err = dao.GetIntegralLogDao().CreateIntegralLog(ctx, iLog)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		return iLog, nil
	})
	if err != nil {
		return nil, parseLueerror(err)
	}
	if ret == nil {
		return nil, nil
	}
	return srv.toIntegralInfoReply(ret.(*model.IntegralLog)), nil
}

// AddConsumptionRecord 新增消费记录
func (srv *assetSrv) AddConsumptionRecord(ctx context.Context, memberId int64, amount uint64) lueerror.LueError {
	mem, err := dao.GetMemberDao().GetMemberById(ctx, memberId)
	if err != nil {
		return lueerror.DbErr(err)
	}
	if mem == nil {
		return nil
	}
	consump := model.AddConsumptionStatistic(mem.Id, mem.BrandId, amount)
	err = dao.GetMemberAccountDao().UpdateConsumptionStat(ctx, consump)
	if err != nil {
		return lueerror.DbErr(err)
	}
	return nil
}

// RollbackConsumptionRecord 退款回滚消费记录
func (srv *assetSrv) RollbackConsumptionRecord(ctx context.Context, memberId int64, amount uint64) lueerror.LueError {
	mem, err := dao.GetMemberDao().GetMemberById(ctx, memberId)
	if err != nil {
		return lueerror.DbErr(err)
	}
	if mem == nil {
		return nil
	}
	// 查询是否存在消费记录
	consum, err := dao.GetMemberAccountDao().GetConsumptionStat(ctx, memberId)
	if err != nil {
		return lueerror.DbErr(err)
	}
	if consum == nil {
		return nil
	}
	if uint64(consum.MoneyAmount) < amount {
		amount = uint64(consum.MoneyAmount)
	}
	consum = model.RollbackConsumptionStatistic(mem.Id, mem.BrandId, amount)
	err = dao.GetMemberAccountDao().UpdateConsumptionStat(ctx, consum)
	if err != nil {
		return lueerror.DbErr(err)
	}
	return nil
}

// 按支付单计算支付单积分额
func (srv *assetSrv) calBalanceByPayOrder(ctx context.Context, payOrderNo string) (uint64, lueerror.LueError) {
	bLogs, err := dao.GetBalanceLogDao().GetBalanceLogByPayOrderNo(ctx, payOrderNo)
	if err != nil {
		return 0, lueerror.DbErr(err)
	}
	var sum int64
	for _, l := range bLogs {
		sum += l.Amount
	}
	if sum < 0 {
		sum = -sum
	}
	return uint64(sum), nil
}

// 添加余额充值流水
func (srv *assetSrv) addBalanceLogByRechargeOrder(ctx context.Context, order *model.RechargeOrder) lueerror.LueError {
	if order == nil {
		return nil
	}
	// 一个充值单只能有一笔记录
	sum, errL := srv.calBalanceByPayOrder(ctx, order.PayOrderNo)
	if errL != nil {
		return errL
	}
	if sum > 0 {
		return nil
	}
	// 获取账号
	acc, errL := srv.getAccountById(ctx, order.MemberId, order.AccId, true)
	if errL != nil {
		return errL
	}
	acc.BalanceRecharge(order.Amount)
	err := dao.GetMemberAccountDao().UpdateAccBalanceById(ctx, acc)
	if err != nil {
		return lueerror.DbErr(err)
	}
	bLog := model.NewBalanceLog(order.BrandId, order.StoreId, order.GroupId, order.CardId, order.MemberId, order.AccId, 0)
	// 金额数量
	if order.IsPayed() {
		bLog.Recharge(order.PayOrderNo, order.Amount)
	} else if order.IsRefunded() {
		// 判断是否超过支付金额
		payAmount, errL := srv.calBalanceByPayOrder(ctx, order.PayOrderNo)
		if errL != nil {
			return errL
		}
		if payAmount < order.RefundAmount {
			return lueerror.BalanceInsufficient(fmt.Errorf("退款金额大于已经支付金额"))
		}
		bLog.RechargeRefund(order.PayOrderNo, order.RefundOrderNo, order.RefundAmount)
	} else {
		// 不可创建流水
		return nil
	}
	bLog.Id, err = dao.GetBalanceLogDao().CreateBalanceLog(ctx, bLog)
	if err != nil {
		return lueerror.DbErr(err)
	}
	return nil
}

func (srv *assetSrv) toBalanceInfoReply(bal *model.BalanceLog) *pb.BalanceInfoReply {
	if bal == nil {
		return nil
	}
	return &pb.BalanceInfoReply{
		BalId:     bal.Id,
		MemberId:  bal.MemberId,
		GroupId:   bal.GroupId,
		BrandId:   bal.BrandId,
		BalType:   bal.GetBalType(),
		Amount:    bal.Amount,
		Status:    "success",
		CreatedAt: util.TimeStdFormat(bal.CreatedAt),
	}
}

// MemberBalancePay 会员余额支付
func (srv *assetSrv) MemberBalancePay(ctx context.Context, memberId, accId int64, p *pb.MemberBalancePayReq) (*pb.BalanceInfoReply, lueerror.LueError) {
	if p == nil {
		return nil, nil
	}
	// 判断支付单是否已经存在, 每个支付单只允许支付一次
	bLogs, err := dao.GetBalanceLogDao().GetBalanceLogByPayOrderNo(ctx, p.PayOrderNo)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if len(bLogs) > 0 {
		return srv.toBalanceInfoReply(bLogs[0]), nil
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 查找账号
		acc, errL := srv.getAccountById(ctx, memberId, accId, true)
		if errL != nil {
			return nil, errL
		}
		if p.BrandId != 0 && acc.BrandId != p.BrandId {
			return nil, lueerror.DirtyData(fmt.Errorf("会员所属品牌与传入不符"))
		}
		// 判断余额
		if acc.Balance < p.Amount {
			return nil, lueerror.BalanceInsufficient(fmt.Errorf("账户 %d 余额不足", accId))
		}
		// 扣款
		acc.Consumption(p.Amount)
		err := dao.GetMemberAccountDao().UpdateAccBalanceById(ctx, acc)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		// 余额流水记录
		bLog := model.NewBalanceLog(acc.BrandId, p.StoreId, acc.GroupId, acc.CardId, acc.MemberId, acc.Id, p.EmpId)
		bLog.Consumption(p.PayOrderNo, p.Amount)
		bLog.Id, err = dao.GetBalanceLogDao().CreateBalanceLog(ctx, bLog)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		return bLog, errL
	})
	if err != nil {
		errL := parseLueerror(err)
		if errL.HttpStatus() != http.StatusInternalServerError {
			return &pb.BalanceInfoReply{
				Status: "failed",
				Reason: errL.Msg(),
			}, nil
		}
		return &pb.BalanceInfoReply{
			Status: "failed",
			Reason: "系统异常",
		}, nil
	}
	if ret == nil {
		return nil, nil
	}
	return srv.toBalanceInfoReply(ret.(*model.BalanceLog)), nil
}

// MemberBalanceRefund 会员余额退款
func (srv *assetSrv) MemberBalanceRefund(ctx context.Context, memberId, accId int64, p *pb.MemberBalanceRefundReq) (*pb.BalanceInfoReply, lueerror.LueError) {
	if p == nil {
		return nil, nil
	}
	// 判断退款单是否已经存在, 每个支付单只允许退款一次
	bLogs, err := dao.GetBalanceLogDao().GetBalanceLogByRefundOrderNo(ctx, p.RefundOrderNo)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	if len(bLogs) > 0 {
		return srv.toBalanceInfoReply(bLogs[0]), nil
	}
	ret, err := dao.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
		// 查询账号
		acc, errL := srv.getAccountById(ctx, memberId, accId, true)
		if errL != nil {
			return nil, errL
		}
		if p.BrandId != 0 && acc.BrandId != p.BrandId {
			return nil, lueerror.DirtyData(fmt.Errorf("会员所属品牌与传入不符"))
		}
		// 计算付款金额，判断可退金额
		payedAmount, errL := srv.calBalanceByPayOrder(ctx, p.PayOrderNo)
		if errL != nil {
			return nil, errL
		}
		if payedAmount < p.Amount {
			return nil, lueerror.RefundBeyond(fmt.Errorf("退款金额大于已经支付金额"))
		}
		// 资金退回账户
		acc.ConsumptionRefund(p.Amount)
		err := dao.GetMemberAccountDao().UpdateAccBalanceById(ctx, acc)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		// 创建流水记录
		bLog := model.NewBalanceLog(acc.BrandId, p.StoreId, acc.GroupId, acc.CardId, acc.MemberId, acc.Id, p.EmpId)
		bLog.ConsumptionRefund(p.PayOrderNo, p.RefundOrderNo, p.Amount)
		bLog.SetReason(p.Reason)
		bLog.Id, err = dao.GetBalanceLogDao().CreateBalanceLog(ctx, bLog)
		if err != nil {
			return nil, lueerror.DbErr(err)
		}
		return bLog, nil
	})
	if err != nil {
		errL := parseLueerror(err)
		if errL.HttpStatus() != http.StatusInternalServerError {
			return &pb.BalanceInfoReply{
				Status: "failed",
				Reason: errL.Msg(),
			}, nil
		}
		return &pb.BalanceInfoReply{
			Status: "failed",
			Reason: "系统异常",
		}, nil
	}
	if ret == nil {
		return nil, nil
	}
	return srv.toBalanceInfoReply(ret.(*model.BalanceLog)), nil
}

// GetBalanceRecords 获取余额记录
func (srv *assetSrv) GetBalanceRecords(ctx context.Context, brandId, memberId int64, pg object.Pagination) (*pb.BalanceLogListReply, lueerror.LueError) {
	group, errL := srv.getBrandGroup(ctx, brandId)
	if errL != nil {
		return nil, errL
	}
	mIds, err := dao.GetLoginChannelDao().GetRelatedMemberId(ctx, brandId, memberId)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	bLogs, total, err := dao.GetBalanceLogDao().GetBalanceLogsByMembersGroup(ctx, mIds, group.Id, pg)
	if err != nil {
		return nil, lueerror.DbErr(err)
	}
	rLogs := make([]*pb.BalanceInfoReply, len(bLogs))
	for idx, bLog := range bLogs {
		rLogs[idx] = srv.toBalanceInfoReply(bLog)
	}
	return &pb.BalanceLogListReply{
		Total:       total,
		PageNo:      pg.PageNo,
		PageSize:    pg.PageSize,
		BalanceLogs: rLogs,
	}, nil
}

// PayOrderFinishNotify 支付成功消息
func (srv *assetSrv) PayOrderFinishNotify(ctx context.Context, p *pay_pb.PayOrderView) lueerror.LueError {
	if p == nil {
		return nil
	}
	switch p.BizType {
	case pay_pb.BizType_BIZ_RECHARGE:
		// 充值处理
		payDetail := object.PayOrderViewPayToOrderDetail(p)
		errL := srv.RechargeOrderPayedNotify(ctx, payDetail)
		if errL != nil {
			return errL
		}
	default:
		// 解析卡信息
		card := &object.CardInfo{}
		if err := json.Unmarshal(p.CardInfo, card); err != nil {
			return lueerror.InvalidRequest(err)
		}
		// 加积分处理
		_, errL := srv.IntegralByPayOrderNotify(ctx, card.MemberId, card.AccId, card.CardId, p)
		if errL != nil {
			return errL
		}
		// 消费统计
		errL = srv.AddConsumptionRecord(ctx, card.MemberId, p.UserPayed)
		if errL != nil {
			return errL
		}
	}
	return nil
}

// RefundOrderFinishNotify 退款消息处理
func (srv *assetSrv) RefundOrderFinishNotify(ctx context.Context, p *pay_pb.RefundOrderView) lueerror.LueError {
	if p == nil {
		return nil
	}
	switch p.BizType {
	case pay_pb.BizType_BIZ_RECHARGE:
		// 待开发
		return nil
	default:
		// 解析卡信息
		card := &object.CardInfo{}
		if err := json.Unmarshal(p.CardInfo, card); err != nil {
			return lueerror.InvalidRequest(err)
		}
		// 退积分处理
		_, errL := srv.IntegralByRefundOrderNotify(ctx, card.MemberId, card.AccId, card.CardId, p)
		if errL != nil {
			return errL
		}
		// 消费统计处理
		errL = srv.RollbackConsumptionRecord(ctx, card.MemberId, p.UserPayed)
		if errL != nil {
			return errL
		}
	}
	return nil
}
